const dataBase = require('../../mysql')

const sql = require('../../mysql/sql')
const verify = require('../../auth/verify')
const verifyRequestMethod = require('../../tool/verifyRequestMethod')
const getNowDate = require('../../component/getNowDate')

/**
 * @description 发布文章
 */
module.exports = function () {
    const that = this
    /**
     * @description 授权
     */
    const authorization = this.request.headers.authorization
    verifyRequestMethod(this.request, 'POST').then(function () {
        /**
         * @description 授权验证
         */
        if (that.isDef(authorization)) {
            return verify(authorization)
        } else {
            return Promise.reject(new Error('缺少Authorization！'))
        }
    }).then(function (options) {
        /**
         * @description 获取body数据
         */
        return that.getBodyData(that.request).then(function (data) {
            return Promise.resolve(Object.assign(data, {
                username: options.username
            }))
        })
    }).then(function (data) {
        /**
         * @description 表单验证
         */
        const body = data
        const type = parseInt(body.type)
        if (!body.articleTitle) {
            that.response.outPrintError('请输入文章标题！')
        } else if (body.articleTitle.trim().length < 4) {
            that.response.outPrintError('标题至少4个字符！')
        } else if (!body.type) {
            that.response.outPrintError('请选择文章类型！')
        } else if (isNaN(type) || (type !== 1 && type !== 2 && type !== 3 && type !== 4)) {
            that.response.outPrintError('无效的文章类型！')
        } else if (!body.textContent) {
            that.response.outPrintError('请输入内容！')
        } else if (body.textContent.length < 30) {
            that.response.outPrintError('内容至少30个字符！')
        } else if (!body.description) {
            that.response.outPrintError('请输入描述！')
        } else if (body.description.length < 12) {
            that.response.outPrintError('描述至少12个字符！')
        }  else if (!body.cover) {
            that.response.outPrintError('封面图不能为空！')
        } else if (that.image.isImageSuffix(body.cover)) {
            that.response.outPrintError('封面图不是一张图片！')
        } else {
            /**
             * @description 更新数据
             */
            return dataBase.connect(sql.SQL_SELECT_ARTICLE_ALL_ID()).then(function (list) {
                return Promise.resolve({
                    list: list,
                    body: body
                })
            })
        }
    }).then(function (options) {
        const list = options.list
        const data = options.body
        let lastArticleId = list[list.length - 1].articleId
        lastArticleId++
        /**
         * @description 配置存储参数
         * @type {any}
         */
        const currentArticle = Object.assign(data, {
            articleId: lastArticleId,
            createTime: getNowDate(),
            authorId: data.username,
            status: 'CHECK'
        })
        return dataBase.connect(
            sql.SQL_CREATE_ARTICLE_ONE(
                currentArticle.articleId,
                currentArticle.articleTitle,
                currentArticle.createTime,
                currentArticle.authorId,
                currentArticle.status,
                currentArticle.description,
                currentArticle.type,
                currentArticle.cover
            )).then(function () {
                return Promise.resolve(currentArticle)
        })
    }).then(function (data) {
        return dataBase.connect(sql.SQL_CREATE_ARTICLE_CONTENT_ONE(data.articleId, data.textContent))
    }).then(function () {
        that.response.outPrintData({}, '发布成功，待审核通过后，即可查看！')
    }).catch(function (e) {
        that.response.outPrintError((e && e.message) || '未知错误！')
    })
}